<?php
namespace Admin\Controller;
use think\console\command\make\Model;
use Think\Controller;
class IndexController extends Controller {
    public function index() {
        $this->display("list");
    }
    public function add() {
       /* 添加,修改成绩成绩*/
        $a=$_GET['id'];
        $count1=$_GET['ud'];
        $select=M('select');
        $personal=M('personal');
        $information=M('information');
        $theuser=M('theuser');
        $teacher=M('teacher');

        for($i=0;$i<$count1;$i++)
        {
            $where['name']=$_POST["xingming{$i}"];
            $sql2=$theuser->where($where)->find();
            $data['id']=$sql2['id'];
            $data['idTwo']=$a;
            $data['condition']=($_POST["chongxiu{$i}"]=='重修'?2:1);
            $data['chengji'] =$_POST["chengji{$i}"];
            $data2['id']=$sql2['id'];
            $data2['idTwo']=$a;
            $sql3=$personal->where($data2)->find();
            if($sql3==true)
            {
                $data2['key']=$sql3['key'];
                if($personal->where($data2)->save($data)==true)
                {
                }else{
                    echo "<script>alert('序号{$i}修改成绩失败了')</script>";
                }
            }else{

                if($personal->add($data)==true)
                {
                    $where2['id']=session('username');
                    $where2['tclass']=$a;
                    $teacher->where($where2)->setField('ispost',1);
                }else{
                    echo "<script>alert('序号{$i}添加成绩失败')</script>";
                }
            }
        }
        $this->redirect('xianshi',array('id'=>$a));
    }

    public function xianshi(){
        /*显示成绩*/
        $a= $_GET['id'];
        $select=M('select');
        $information=M('information');
        $count1=$select->field("a.iname")->where("a.idTwo=b.class AND b.class=$a")->table(array( 'information'=>a,'select'=>b))->count();
        $arr=$select->field("a.iname")->where("a.idTwo=b.class AND b.class=$a")->table(array('information'=>a,'select'=>b))->select();
        $theuser=M('theuser');
        //$count2=$select->field("a.name,a.sex,a.xueyuan,b.sid AND b.class=$a")->where("a.id=b.tname")->table(array('theuser'=>a,'select'=>b))->select();
        $arr2=$select->field("a.name,a.sex,a.xueyuan,b.sid,a.id")->where("a.id=b.tname AND b.class=$a")->table(array('theuser'=>a,'select'=>b))->select();
        $personal=M('personal');
        $str[]=array();
        $at[]=array();
        $menu[]=array();
        for($i=0;$i<$count1;$i++)
        {
            $data2['id']=$arr2[$i]['id'];
            $data2['idTwo']=$a;

            $sql3=$personal->where($data2)->find();
            if($sql3['chengji']>=60)
            {
                $menu[$i]="<span style='color: black'>{$sql3['chengji']}</span>";
            }else{
                $menu[$i]="<span style='color: red'>{$sql3['chengji']}</span>";
            }
            $str[$i]=($arr2[$i]['sex']==0?"男":"女");
            $at[$i]=$arr2[$i]['sid']==1?"重修":"初选";
        }
        $lianjie="<a href=\"../../chengjixiugai.html?id={$a}\">成绩修改界面</a>";
        $this->assign(lianjie,$lianjie);
        $this->assign(menu,$menu);
        $this->assign('th',$a);
        $this->assign(arr2,$arr2);
        $this->assign(at,$at);
        $this->assign(str,$str);
        $this->assign(arr,$arr);
        $this->assign(count1,$count1);
        $this->display("xianshi");
    }

    public function update() {
        print_r($_POST);
        $theuser=M('theuser');
        $value = session('username');
        $where['username']=$value;
        $pass=session('password');
        $sql=$theuser->where($where)->find();
        //var_dump($sql);
        $str="";
        switch ($sql['sex'])
        {
            case 0:
                $str="男";
                break;
            case 1:
                $str="女";
                break;
        }

        if($_POST)
        {
            $user = D('theuser');
            $where['username'] = session('username');
            $map['id']=$_POST['id'];
            $map['password']=$_POST['pass1'];
            $map['email']=$_POST['email'];
            $map['tel']=$_POST['tel'];
    
            if($_POST['pass1']==$_POST['pass2'])
            {
                $this->assign(sql,$sql);
                $this->assign(str,$str);
                if ($user->where($where)->save($map)) {
                    $this->assign(a,'修改成功');
                    if($pass==$_POST['pass1'])
                    {
                        $this->display(T('Home@shouye/page1'));
                    }else{
                        session('[destroy]');
                        $this->redirect("Home/Index/index");
                    }
                } else {
                    $this->assign(a,$user->getError());
                    $this->display('update','utf-8');
                }
            }else {
                echo "<script>alert('两次密码输入不一致');history.back(-1)</script>";
            }


        }else{
            $this->assign(sql,$sql);
            $this->assign(str,$str);
            $this->display('update','utf-8');
        }
    }

    public function chengjixiugai(){
        $a= $_GET['id'];
        $select=M('select');
        $information=M('information');
        $count1=$select->field("a.iname")->where("a.idTwo=b.class AND b.class=$a")->table(array( 'information'=>a,'select'=>b))->count();
        $arr=$select->field("a.iname")->where("a.idTwo=b.class AND b.class=$a")->table(array('information'=>a,'select'=>b))->select();
        $theuser=M('theuser');
        //$count2=$select->field("a.name,a.sex,a.xueyuan,b.sid AND b.class=$a")->where("a.id=b.tname")->table(array('theuser'=>a,'select'=>b))->select();
        $arr2=$select->field("a.name,a.sex,a.xueyuan,b.sid,a.id")->where("a.id=b.tname AND b.class=$a")->table(array('theuser'=>a,'select'=>b))->select();
        $personal=M('personal');
        $str[]=array();
        $at[]=array();
        $menu[]=array();
        for($i=0;$i<$count1;$i++)
        {
            $data2['id']=$arr2[$i]['id'];
            $data2['idTwo']=$a;

            $sql3=$personal->where($data2)->find();
            $menu[$i]=$sql3['chengji'];

            $str[$i]=($arr2[$i]['sex']==0?"男":"女");
            $at[$i]=$arr2[$i]['sid']==1?"重修":"初选";
        }
        $this->assign(menu,$menu);
        $this->assign('th',$a);
        $this->assign(arr2,$arr2);
        $this->assign(at,$at);
        $this->assign(str,$str);
        $this->assign(arr,$arr);
        $this->assign(count1,$count1);
        $this->display('chengjixiugai','utf-8');
    }
}